package com.hnkywl.sts.dao.tjbb;

import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import oracle.jdbc.OracleCallableStatement;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: SDE09
 * Date: 13-9-28
 * Time: 下午5:14
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class YjykhhzbDao extends SimpleHibernateTemplate {

    public Map<String, List> yjykhhzList(Integer year, Integer month) {
        int mons;
        if (month < 10) {
            mons = Integer.parseInt(String.valueOf(year) + ("0" + String.valueOf(month)));
        } else {
            mons = Integer.parseInt(String.valueOf(year) + String.valueOf(month));
        }
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call P_RPT_JYKHHZ_MON(?,?)}");
            call.setInt(1, mons);
            call.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            //====================================
            rs = ((OracleCallableStatement) call).getCursor(2);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, List> result = new HashMap<String, List>();
            List<Map> rows = new ArrayList<Map>();
            List<Map> footer = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                if (!"合计".equals(r.get("gs_name").toString()))
                    rows.add(r);
                else
                    footer.add(r);
            }
            result.put("rows", rows);
            result.put("footer", footer);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }

    private void closeResource(ResultSet rs, Statement s, Connection con) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (s != null)
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (con != null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
    }


    public Map<String, Object> sjsrList(Integer gsid, Integer year, Integer month, Integer pageNo, Integer pageSize) {
        //1、gs_id 若为空  则等于 -1
        if (gsid == null)
            gsid = -1;
        Integer v_gsid;
        v_gsid = gsid;
        int mons;
        if (month < 10) {
            mons = Integer.parseInt(String.valueOf(year) + ("0" + String.valueOf(month)));
        } else {
            mons = Integer.parseInt(String.valueOf(year) + String.valueOf(month));
        }
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        Integer total = 0;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call P_RPT_JYKHHZ_MON_JYSR(?,?,?,?,?,?)}");
            call.setInt(1, gsid);
            call.setInt(2, mons);
            call.setInt(3, (pageNo - 1) * pageSize);
            call.setInt(4, pageNo * pageSize);
            call.registerOutParameter(5, Types.NUMERIC);
            call.registerOutParameter(6, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            //====================================
            total = call.getInt(5);
            rs = ((OracleCallableStatement) call).getCursor(6);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, Object> result = new HashMap<String, Object>();
            List<Map> rows = new ArrayList<Map>();
            List<Map> footer = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                if (!"合计".equals(r.get("gs_name").toString()))
                    rows.add(r);
                else
                    footer.add(r);
            }
            result.put("total", total);
            result.put("rows", rows);
            result.put("footer", footer);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }


    public Map<String, Object> zjcbList(Integer gsid, Integer year, Integer month) {
        //1、gs_id 若为空  则等于 -1
        if (gsid == null)
            gsid = -1;
        Integer v_gsid;
        v_gsid = gsid;
        int mons;
        if (month < 10) {
            mons = Integer.parseInt(String.valueOf(year) + ("0" + String.valueOf(month)));
        } else {
            mons = Integer.parseInt(String.valueOf(year) + String.valueOf(month));
        }
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        Integer total = 0;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call P_RPT_JYKHHZ_MON_ZJZYMX(?,?,?)}");
            call.setInt(1, mons);
            call.setInt(2, gsid);
            call.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            //====================================
            rs = ((OracleCallableStatement) call).getCursor(3);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, Object> result = new HashMap<String, Object>();
            List<Map> rows = new ArrayList<Map>();
            List<Map> footer = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                if (!"合计".equals(r.get("day").toString()))
                    rows.add(r);
                else
                    footer.add(r);
            }
            result.put("rows", rows);
            result.put("footer", footer);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }
}
